<template>
  <div class="dataScreen-container">
    <div class="data-screen" ref="dataScreenRef">
      <slot />
    </div>
  </div>
</template>


<script lang="ts" setup>
import { onBeforeUnmount, onMounted, ref } from 'vue'
const dataScreenRef = ref<HTMLElement | null>(null)

const width = 1920
const height = 1080

// const width = 1920;
// const height = 1080;

onMounted(() => {
  // 初始化时为外层盒子加上缩放属性，防止刷新界面时就已经缩放
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale(
      width,
      height
    )}) translate(-50%, -50%)`
    dataScreenRef.value.style.width = `${width}px`
    dataScreenRef.value.style.height = `${height}px`
  }
  /* 初始化echarts */
  // initCharts();
  // 为浏览器绑定事件
  window.addEventListener('resize', resize)
})

/* 根据浏览器大小推断缩放比例 */
const getScale = (_width = width, _height = height) => {
  let ww = window.innerWidth / _width
  let wh = window.innerHeight / _height
  return ww < wh ? ww : wh
}

/* 浏览器监听 resize 事件 */
const resize = () => {
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`
  }
  // 使用了 scale 的echarts其实不需要需要重新计算缩放比例
  // Object.values(dataScreen).forEach((chart) => {
  //   chart && chart.resize();
  // });
}

/* 销毁时触发 */
onBeforeUnmount(() => {
  window.removeEventListener('resize', resize)
  // clearInterval(timer);
  // 每次离开页面时，清空echarts实例，不然会出现无法显示的问题
  // Object.values(dataScreen).forEach(val => {
  // 	val?.dispose();
  // });
})
</script>

<style lang="less" scoped>
.dataScreen-container {
  width: 100%;
  height: 100%;
  // background: url("./images/bg.png") no-repeat;
  // background-attachment: fixed;
  // background-position: center;
  // background-repeat: no-repeat;
  // background-size: 100% 100%;
  // background-size: cover;

  .data-screen {
    // z-index: 0;
    // background: #fff;
    position: fixed;
    top: 50%;
    left: 50%;
    // z-index: 999;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.3s;
    transform-origin: left top;
  }
}
</style>
